<?php
namespace app\app\controller\v1;
use app\app\Appbase;
use app\admin\model\SystemParamValue as ParamValue;
use app\common\model\Shop;
use app\common\model\Goods;
use app\common\model\GoodsSeckill;
use app\common\model\ActivitySeckill;
use app\common\model\News;
use base\map\AmapApi;

class Index extends Appbase 
{
	
	/**
	* 首页
	* /app/v1/Index/index
	* 需要登录 
	* @is_need_login true
	*/
	public function index($lat,$lng){
		if(!$lat || !$lng){
			return $this->ReError('参数错误！');	
		}
		$shop_id = request()->shopId;
		try {
			$componentsData = getSystem('system_fixtures_json',$shop_id) ?: '[]';
			$data = json_decode($componentsData,true);
			
			$shop =  Shop::where('id',$shop_id)
			        	->field('id,name,region,address_detail,latitude,longitude')
			        	->field('img,banners,phone,remark')
			        	->field("concat(province,city,address_detail) as addr")
			        	->find() ?: [];
			
			foreach($data as $key => $val){
				
				//TODO 首页banner图
				if($val['type'] == 'banner'){
		        	$banner = ParamValue::getSlides('buess_index_banner','shop_id_'.$shop_id);
		       		if( !$banner[0] ){
		        		$banner = ParamValue::getSlides('buess_index_banner','shop_id_0');
		        	}
					$data[$key]['data'] = $banner;
				}
				
				//TODO 当前门店
				if($val['type'] == 'yykcurrent'){
					$data[$key]['data'] = $shop;
				}
				
				//TODO 推荐技师
				if($val['type'] == 'yykarticifer'){
					$data[$key]['data'] = $shop->staff()
		        		->where([ 'status'=>1, 'is_del'=>0, 'is_good'=>1])
		        		->field("id,name,ifnull(img,'') as avatar,work_type as job")
		        		->withAvg(['evaluate'=>function($query){
						    $query->where('is_del',0)->field('staff_id,service_score');
						    return 'star';
						}],'service_score')
						->withAvg(['yyOrder'=>function($query){
						    $query->where('status',1)->field('star');
						    return 'star';
						}],'star')
						->withCount(['yyOrder'=>function($query){
						    $query->where('is_del',0);
						    return 'order_count';
						}])
						->order('sort_no')
		        		->select() ?: [];
				}

				//TODO 推荐商品
				if($val['type'] == 'yykgoods'){
					$data[$key]['data'] =  $shop->getGoodsModel()
	        			->where('is_good',1)
	        			->page(1,4)
	        			->all();
				}

				//TODO 限时抢购
				if($val['type'] == 'yykflash'){
					$data[$key]['data'] =  $shop->getGoodsModel()
		        		->where('is_benefit',1)
		        		->where('type','in','0,1,3,4')
		        		->where('benefit_end_at','> time', date("Y-m-d H:i:s"))
		        		->where('benefit_start_at','< time', date("Y-m-d H:i:s"))
		        		->page(1,4)
		        		->all();
				}

				//TODO 附近店铺
				if($val['type'] == 'yykshop'){
					$data[$key]['data'] =  Shop::getNearbyShop($lat,$lng,$shopId,$name,0,1,5);
				}
				
			}

			return $this->ReSucess($data);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 获取推荐商品列表
	* /app/v1/Index/recommendGoods
	* 需要登录 
	* @is_need_login true
	*/
	public function recommendGoods(){
		try {
	        $data = model('goods')->getModel()->where('is_good',1)->all();
			return $this->ReSucess($data);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 获取抢购商品列表
	* /app/v1/Index/recommendGoods
	* 需要登录 
	* @is_need_login true
	*/
	public function snapGoods(){
		try {
	        $data = model('goods')->getModel()
	        		->where('is_benefit',1)
	        		->where('type','in','0,1,3,4')
	        		->where('benefit_end_at','> time', date("Y-m-d H:i:s"))
	        		->where('benefit_start_at','< time', date("Y-m-d H:i:s"))
	        		->all();
			return $this->ReSucess($data);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 获取推荐员工列表
	* /app/v1/Index/staffList
	* 需要登录 
	* @is_need_login true
	*/
	public function staffList(){
		$shop_id = request()->shopId;
		try {
			$shop = Shop::get($shop_id);
	        $data = $shop->staff()
	        		->where(['status'=>1, 'is_del'=>0])
	        		->field("id,name,ifnull(img,'') as avatar,work_type as job")
	        		->withAvg(['evaluate'=>function($query){
					    $query->where('is_del',0)->field('staff_id,service_score');
					    return 'star';
					}],'service_score')
					->withAvg(['yyOrder'=>function($query){
					    $query->where('status',1)->field('star');
					    return 'star';
					}],'star')
					->withCount(['yyOrder'=>function($query){
					    $query->where('is_del',0);
					    return 'order_count';
					}])
					->page(input('pageIndex') ?: 1,input('pageSize') ?: 20)
					->order('is_good,sort_no')
	        		->select();
			return $this->ReSucess($data);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 获取附近商家
	* /app/v1/Index/getNearbyShop
	* 需要登录 
	* @is_need_login true
	*/
	public function getNearbyShop($lat,$lng,$name='',$cate_id=0){
		$shopId = request()->shopId;
		try {
			//TODO 当前地址
			$address = AmapApi::getAddress($lat,$lng);
			//TODO 店铺列表
			$shop_list = Shop::getNearbyShop($lat,$lng,$shopId,$name,$cate_id);
			//TODO 返回
			return $this->ReSucess( compact('address','shop_list') );	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	

	
}



